using System;
using System.Collections.Generic;
using System.Text;
using Madason.GenUtils;

namespace Madason.MetaModel.Dump
{
	class Program
	{

		static string indent(int n)
		{
			string buffer = "";
			for (int i = 0; i < n; i++)
				buffer += "  ";
			return buffer;
		}


		static string GetAttributeType(Madason.MetaModel.Attribute attr)
		{
			if (attr.Type != null)
				return attr.Type.Name;
			else
				return "?";
		}

		static void DumpAttribute(Madason.MetaModel.Attribute attr, int level)
		{
			string line = indent(level) + "[A] ";
			if (attr.StereotypeName != null && attr.StereotypeName != "")
				line += "<<" + attr.StereotypeName + ">> ";

			line += attr.Name + " : " + GetAttributeType(attr) + " ";

			foreach (TaggedValue tv in attr.TaggedValues)
				line += tv.Key + "='" + tv.Value + "' ";

			Console.Out.WriteLine(line);
		}


		static void DumpClass(Class cls, int level)
		{
			string line = indent(level) + "[C] ";
			if (cls.StereotypeName != null && cls.StereotypeName != "")
				line += "<<" + cls.StereotypeName + ">> ";
			line += cls.Name + " ";

			foreach (TaggedValue tv in cls.TaggedValues)
				line += tv.Key + "='" + tv.Value + "' ";

			Console.Out.WriteLine(line);

			foreach (Madason.MetaModel.Attribute attr in cls.Attributes)
				DumpAttribute(attr, level + 1);
		}

		static void DumpNameSpace(Namespace ns, int level)
		{
			foreach (Namespace child in ns.Namespaces)
			{
				string line = indent(level);
				if (child.StereotypeName != null && child.StereotypeName != "")
					line += "<<" + child.StereotypeName + ">> ";
				line += child.Name + " ";


				foreach (TaggedValue tv in child.TaggedValues)
					line += tv.Key + "='" + tv.Value + "' ";
				
				Console.Out.WriteLine(line);

				
				DumpNameSpace(child, level + 1);
			}

			foreach (Class cls in ns.Classes)
				DumpClass(cls, level + 1);
		}

		static void Main(string[] args)
		{
			GeneratorWorkflow workflow = new GeneratorWorkflow(args);

			workflow.CheckCommandLine();
			workflow.LoadModel();

			DumpNameSpace(workflow.Model, 0);			
		}
	}
}
